<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="pdepartmenting-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
		<el-table :data=" employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="用户名" width="150" sortable>
			</el-table-column>
			<el-table-column prop="age" label="年龄" width="150" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" width="150" sortable>
			</el-table-column>
			<el-table-column prop="phone" label="电话号码" width="150" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="150" sortable>
				<template scope="scope">
					<span style="color: green" v-if="scope.row.state === 1">启用</span>
					<span style="color: red" v-else-if="scope.row.state === 0">禁用</span>
					<span style="color:dimgray" v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :current-page="currentPage"
						   :page-size="pageSize"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面
			:visible.sync= 控制模态框是否显示 为true就显示
			:rules  绑定模型层的属性校验规则
			prop= 用来申明校验规则的属性名
			ref= 给当前表单取一个别名 方便后面表单提交的时候，做整体校验
		-->
		<el-dialog :title="title" :visible.sync="employeeFormVisible" :close-on-click-modal="false">
			<el-form :model="employeeForm" label-width="80px" :rules="employeeFormRules" ref="employeeForm">
				<el-form-item label="用户名" prop="username">
					<el-input v-model="employeeForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="employeeForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="年龄" prop="age">
					<el-input v-model="employeeForm.age" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话号码" prop="phone">
					<el-input v-model="employeeForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="employeeForm.state">
						<el-radio class="radio" :label="1">启用</el-radio>
						<el-radio class="radio" :label="0">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="employeeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="departmentSubmit" :loading="employeeLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
    export default {
        data() {
            return {
                title:"",
                filters: {
                    keyword: ''
                },
                employees:[],//管理员展示
                total: 0,
                currentPage:1,
                pageSize: 4,
                listLoading: false,
                sels: [],//列表选中行

                employeeFormVisible: false,//新增界面是否显示
                employeeLoading: false,
                employeeFormRules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '请输入手机号', trigger: 'blur' },
						{}
                    ],
                },
                //新增界面数据
                employeeForm: {
                    id:null,
                    username:'',
                    email:'',
                    phone:'',
                    salt:'',
                    password:'',
                    age:null,
                    state:null
                }

            }
        },
        methods: {
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getEmployees();
            },
			search(){
              this.currentPage=1;
              this.getEmployees();
			},
            //获取员工列表
            getEmployees(){
                this.$http.get("/emp")
                    .then(result=>{
                        this.employees=result.data;
                    })
                    .catch(result=>{
                        this.$message({
                            message: '操作失败！',
                            type: 'error'
                        });
                    })
            },
            //获取部门列表
            getEmployees() {
                let para = {//请求参数
                    currentPage: this.currentPage,
                    pageSize: this.pageSize,
                    keyword: this.filters.keyword
                };
                this.listLoading = true;//开启忙等框
                //这里发送axios请求从后端拿数据
                this.$http.post("/emp",para)
                    .then(result=>{
                        this.listLoading = false;//关闭忙等框
                        result = result.data;//PageList
                        console.log(result);
                        this.employees = result.data;
                        this.total = result.total;

                    })
                    .catch(result=>{
                        this.$message({
                            message: '操作失败！',
                            type: 'error'
                        });
                    })

            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {//点击确定按钮
                    this.listLoading = true;//开启忙等框
                    this.$http.delete("/emp/"+ row.id)
                        .then(result=>{
                            //3.关闭忙等框
                            this.listLoading = false;//关闭忙等框
                            result = result.data;//AjaxResult
                            //1.要提示后端业务执行到底是：成功还是失败
                            if(result.success){
                                this.$message({
                                    message: '删除成功！',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
                            }
                            //4.定位到第一页 要？ 如果不要会不会有问题？
                            this.currentPage = 1;
                            //2.刷新页面
                            this.getEmployees();

                        })
                        .catch(result=>{
                            console.log(result);
                            this.$message({
                                message: '操作失败',
                                type: 'error'
                            });
                        });
                }).catch(() => {//点击取消

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                //获取部门树
                this.title = "编辑";
                this.employeeFormVisible = true;//控制新增或者编辑的模态框的显示
                // Object.assign 克隆
                this.employeeForm = Object.assign({}, row);
                console.log(this.employeeForm);

            },
            //显示新增界面
            handleAdd: function () {
                this.title = "新增";
                this.employeeFormVisible = true;//控制新增或者编辑的模态框的显示
                this.employeeForm = {//清空历史遗留数据
                    id:null,
                    username:'',
                    email:'',
                    phone:'',
                    salt:'',
                    password:'',
                    age:null,
                    state:null
                };
            },
            //新增/编辑
            departmentSubmit: function () {
                this.$refs.employeeForm.validate((valid) => {
                    if (valid) {//表单校验成功
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.employeeLoading = true;//开启提交按钮忙等框
                            let param = Object.assign({}, this.employeeForm);
                            //发送请求
                            this.$http.put("/emp",param)
                                .then(result=>{
                                    result = result.data;
                                    this.employeeLoading = false;//关闭提交按钮忙等框
                                    if(result.success){
                                        this.employeeFormVisible = false;
                                        //刷新页面
                                        this.getEmployees();
                                        this.$message({
                                            message: '操作成功！！！',
                                            type: 'success'
                                        });
                                    }else {
                                        this.$message({
                                            message:result.msg,
                                            type: 'error'
                                        });
                                    }
                                }).catch(result=>{
                                console.log(result)
                                alert("出错了！！！")
                            })
                        });
                    }
                });
            },
            selsChange: function (sels) {//每点击一次复选框，会把当前页面上所选中的所有行的数据封装到sels
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);//[1,2,3]
                this.$confirm('确认删除选中记录吗？', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/emp",ids)
                        .then(result=>{
                            //3.关闭忙等框
                            this.listLoading = false;//关闭忙等框
                            result = result.data;//AjaxResult
                            //1.要提示后端业务执行到底是：成功还是失败
                            if(result.success){
                                this.$message({
                                    message: '删除成功！',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.msg,
                                    type: 'error'
                                });
                            }
                            //4.定位到第一页 要？ 如果不要会不会有问题？
                            this.currentPage = 1;
                            //2.刷新页面
                            this.getEmployees();
                        })
                        .catch(result=>{
                            console.log(result);
                            this.$message({
                                message: '操作失败',
                                type: 'error'
                            });
                        });
                }).catch(() => {

                });
            }
        },
        mounted() {
            //获取员工列表
            this.getEmployees();

        }
    }

</script>

<style scoped>

</style>